#include<bits/stdc++.h>
using namespace std;
#define int long long
int flag[3000][3000];
const int M = 520010;
int n,m,s,t;
int h[M],e[M],ne[M],w[M];
int idx = 2;
int ans = 0;
int vis[M],dis[M],pre[M];
void add(int a,int b,int c)
{
    e[idx] = b;
    w[idx] = c;
    ne[idx] = h[a];
    h[a] = idx ++;
    e[idx] = a;
    w[idx] = 0;
    ne[idx] = h[b];
    h[b] = idx ++;
}
int bfs()//找增广路 
{
    for(int i = 1; i <= n; i ++) vis[i] = 0;
    queue<int>q;
    q.push(s);
    vis[s] = 1;
    dis[s] = 0x3f3f3f3f;
    while(q.size())
    {
        int x = q.front();
        q.pop();
        for(int i = h[x]; i != -1; i = ne[i])
        {
            if(w[i] == 0) continue;
            int v = e[i];
            if(vis[v] == 1) continue;
            dis[v] = min(dis[x],w[i]);
            pre[v] = i;
            q.push(v);
            vis[v] = 1;
            if(v == t) return 1;
        }
    }
    return 0;
}
void update()
{
    int x = t;
    while(x != s)
    {
        int v = pre[x];
        w[v] -= dis[t];
        w[v ^ 1] += dis[t];
        x = e[v ^ 1];
    }
    ans += dis[t];
}
signed main(){
    cin >> n >> m >> s >> t;
    memset(h,-1,sizeof h);
    for(int i = 1; i <= m; i ++)
    {
        int u,v,c;
        cin >> u >> v >> c;
        if(flag[u][v] == 0)//判别重复边
        {
            add(u,v,c);
            flag[u][v] = idx;
        }
        else
        {
            w[flag[u][v] - 2] += c;
        }
    }
    while(bfs()!= 0) //直到网络中不存在增广路 
    {
        update();
    }
    cout << ans << endl;
}